<template>
<layoutBlock >
  <div class="content">
    <div class="">
<!--      <Select filterable v-model="condition.car_type_code" clearable placeholder="请选择类型" @on-change="getData">-->
<!--        <Option v-for="item in statusList" :value="item.car_type_code" :key="item.car_type_code">{{ item.car_type_name }}</Option>-->
<!--      </Select>-->
            <Input class="mb10" search v-model="condition.keyword" placeholder="请输入项目名称" @on-search="getData" />
      <Button class="ml10 fr"  @click="getEdit('',true)"><Icon type="md-add" />新增项目</Button>
      <Button class="ml10 fr" type="error" @click="delAll">删除</Button>
    </div>
    <Table :loading="loading" @on-selection-change="selectList" border class="mt20" ref="selection" :columns="columns2" :data="data1">

      <template slot-scope="{ row, index }" slot="regdate" >
        <span>{{moment(row.regdate).format("YYYY-MM-DD")}}</span>
      </template>
      <template slot-scope="{ row, index }" slot="action" >
        <span class="cursor cr2d8cf0" @click="getEdit(row,false)">编辑</span>
        <span class="cursor cr2d8cf0" @click="getDel(row,1)">删除</span>
      </template>
    </Table>
    <div class="disflex mt30">
      <Page :total="total" v-if="total>condition.limit"  :current="condition.start" show-elevator @on-change="changepage"></Page>
    </div>
    <Modal :mask-closable="false" footer-hide  v-model="isShow" :title="isAdd?'新增项目':'编辑项目'" width="500">
      <projectEditor :orgList="orgList" :list="statusList" :isAdd="isAdd" :editorData="editorData" v-if="isShow" @success="success"></projectEditor>
    </Modal>
    <Modal
        v-model="isShow2"
        title="删除"
        @on-ok="getDel('',2)"
        @on-cancel="cancel" width="350">
      <div class="input-item">
        该项目将被删除, 是否继续?
      </div>
    </Modal>
  </div>
</layoutBlock>
</template>
<script>
import projectEditor from '../../components/project/projectEditor';
import {projectType,projectList,projectDel} from '@/api/project';
import {regionTree,orgTree} from '@/api/base'
import tool from "../../libs/tool";
import base from '@/mixins/base';
export default {
  mixins: [base],
  components:{projectEditor},
  data () {
    return {
      loading:false,
      total:0,//总条数
      roleId:'',
      editorData:{},
      title:'编辑',
      statusList:[],
      condition:{//头部筛选传参
        keyword:'',
        limit:10,
        start:1,
        is_enable:'',
      },
      columns2: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '项目名称',
          key: 'name',
          ellipsis:true
        },
        {
          title: '所属单位',
          key: 'orgName',
          ellipsis:true
        },
        {
          title: '项目编码',
          key: 'code',
          ellipsis:true
        },
        {
          title: '项目地址',
          key: 'address',
          ellipsis:true
        },
        {
          title: '成立时间',
          slot: 'regdate',
          align: 'center'
        },
        {
          title: '操作',
          slot: 'action',
          width: 120,
          align: 'center'
        }
      ],
      data1: [],
      isShow:false,
      isShow2:false,
      isShow3:false,
      isShow4:false,
      detail:'',
      ids:'',
      isAdd:true,
      noShow:false,
      userId:'',
      isShowUser:false,
      selectlist:[],
      orgList:[],

    }
  },
  created(){
    // this.getType();
    this.getData();
    this.getRegionTree();
    this.getOrgTree();
  },
  filters:{
    format(arr){//这里不能用类似与formatDepts 驼峰命名 不然模版中使用会编译报错
      let res= (arr||[]).map(item=>{
        return item.name
      })
      return res.join('、')
    },
  },
  methods: {
   async getOrgTree(){
     let res= await orgTree({});
     this.orgList=tool.formatTree(_.get(res,'data')||[],'children','lable','id',false,false);
   },
   async getRegionTree() {
     let res= await regionTree({});
     this.statusList=tool.formatTree(_.get(res,'data')||[],'children','lable','id',false,false);

    },
    async getType(){
      let res = await projectType({});
      this.statusList= _.get(res,'data')||[];
    },
    selectList(value){
      this.condition.start=1;
      this.selectlist=value
    },
    delAll(){
      let arr= this.selectlist.map(item=>{
        return item.id
      })
      if(arr.length==0){
        this.$Message.warning('请先选择项目！')
        return
      }
      this.getDel({id:arr.join()},1)
    },
    success(){
      this.getData();
      this.isShow=false;
      this.isShow03=false;
    },
    //获取列表
    async getData(){
      this.loading=true;
      let  res =!this.orgIds?this.defaultListData: await  projectList({...this.condition ,pOrgId:this.orgIds});
      this.loading=false;
      this.data1= _.get(res,'data.records') ||[];
      this.total=_.get(res,'data.total')||0;
    },
    //分页projectType
    changepage(page){
      this.condition.start = page
      this.getData()
    },
    getEdit(row,type){
      this.isAdd=type;
      this.isShow = true;
      if(type) return;
      let {address,bz,name,id,orgId,regdate} = row;
      let regionCode=[];
      if(row.province) regionCode.push(row.province);
      if(row.city) regionCode.push(row.city);
      if(row.district) regionCode.push(row.district);
      this.editorData={address,bz,name,id,orgId,regdate,regionCode}
    },
    async getDel(row,type){
      if(type==1){
        this.ids = row.id
        this.isShow2 = true;
      }else{
        let res = await projectDel({projectIds:this.ids});
        if(_.get(res,'code')==0){
          this.isShow2 = false;
          this.$Message.success('删除成功')
          this.getData();
        }else{
          this.$Message.error(_.get(res,'message')||'删除失败！')
        }
      }
    },

    cancel(){},
  }
}
</script>

<style scoped lang="less">

</style>
